﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TouchDriver.Data;
namespace TouchDriver
{
    public class TouchDriver
    {
        private bool aligned = false;
        private ComReader reader = ComReader.GetInstance();
        private float K = 0;
        private float B = 0;

        //鼠标左击事件
        public delegate void MouseClickHandler(Location loc);
        public event MouseClickHandler MouseClickEvent;

        //一个手指移动事件
        public delegate void SingleMoveHandler(Location begin, Location end);
        public event SingleMoveHandler SingleMoveEvent;

        //两个手指移动事件
        public delegate void DoubleMoveHandler(Record begin, Record end);
        public event DoubleMoveHandler DoubleMoveEvent;

        //校准结束
        public delegate void AlignCompletedHandler(bool aligned);
        public event AlignCompletedHandler AlignCompletedEvent;

        public TouchDriver()
        {
            
        }

        void reader_DoubleMoveEvent(Record begin, Record end)
        {
            //DoubleMoveEvent(RawToScreen(begin), RawToScreen(end));
            DoubleMoveEvent(begin, end);
        }

        void reader_SingleMoveEvent(Location begin, Location end)
        {
            //SingleMoveEvent(RawToScreen(begin), RawToScreen(end));
            SingleMoveEvent(begin, end);
        }

        void reader_MouseClickEvent(Location loc)
        {
            //MouseClickEvent(RawToScreen(loc));
            MouseClickEvent(loc);
        }

        

        /// <summary>
        /// 将厘米坐标转换成屏幕的像素坐标
        /// </summary>
        /// <param name="raw">
        /// 厘米坐标
        /// </param>
        /// <returns>
        /// 像素坐标
        /// </returns>
        private Record RawToScreen(Record raw)
        {
            Record result = new Record();
            result.loc1 = RawToScreen(result.loc1);
            result.loc2 = RawToScreen(result.loc2);

            return result;
        }

        private Location RawToScreen(Location raw)
        {
            Location result = new Location();
            result.x = (int)(K * raw.x + B);
            result.y = (int)(K * raw.y + B);
            return raw;
        }

        /// <summary>
        /// 屏幕校准
        /// </summary>
        private void Align()
        {
            //TODO
            AlignForm form = new AlignForm();
            if (form.ShowDialog() == System.Windows.Forms.DialogResult.Yes)
            {
                K = form.K;
                B = form.B;

                AlignCompletedEvent(true);
                aligned = true;

                reader.MouseClickEvent += new ComReader.MouseClickHandler(reader_MouseClickEvent);
                reader.SingleMoveEvent += new ComReader.SingleMoveHandler(reader_SingleMoveEvent);
                reader.DoubleMoveEvent += new ComReader.DoubleMoveHandler(reader_DoubleMoveEvent);
            }
            else
            {
                AlignCompletedEvent(false);
            }
        }

        public void Start()
        {
            if(!aligned)
            {
                //Align();
                aligned = true;
            }

            if (aligned)
            {
                reader.Start();
            }
        }
    }
}
